<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Galeri extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->_data["menuHL"] = "Galeri";
        $this->addBreadCrumb("Galeri", site_url("galeri"));
    }

    public function index() {
        redirect("galeri/home/populer");
    }

    public function home($sortby = "populer") {
        $this->_data["pageTitle"] = "Galeri Perdesaan Sehat";
        $this->_data["sortby"] = $sortby;
        $this->_data["titleFB"] = "Galeri Perdesaan Sehat";
        $this->load->model("Gallery_model");
        $hots = $this->Gallery_model->fetchHot(0);
        if (count($hots) > 0) {
            $this->_data["imageFB"] = $hots[0]->src;
        }
        switch ($sortby) {
            case "populer" :
                $this->_data["showMoreCode"] = "galpop";
                $this->addBreadCrumb("Populer", site_url("galeri/home/populer"));
                break;
            case "baru" :
                $this->_data["showMoreCode"] = "galbar";
                $this->addBreadCrumb("Terbaru", site_url("galeri/home/baru"));
                break;
        }
        $this->_renderPage("galeri/home");
    }

    public function lihat($id = 0) {
        $this->_data["pageTitle"] = "Galeri";
        $this->load->model("Gallery_model");
        $galObj = $this->Gallery_model->findById($id);

        if ($galObj != NULL) {
            $this->_data["gal"] = $galObj;
            $this->_data["titleFB"] = $galObj->judul;
            $this->_data["imageFB"] = $galObj->src;
            $this->_data["descFB"] = "";

            $this->addBreadCrumb($galObj->judul, site_url("galeri/lihat/" . $id));
            $this->_renderPage("galeri/detail");
        } else {
            redirect("home/error");
        }
    }

    public function upload($type) {
        $authorized = Array("gal_contributor");
        if ($this->isAuth($authorized)) {
            switch ($type) {
                case "img" :
                    $this->load->model("Gallery_model");
                    echo $this->Gallery_model->createImage("IMG", $this->input->post("gambar"), $this->input->post("judul"));
                    break;
                case "vid" :
                    //...
                    break;
            }
        }
        exit;
    }

    public function updateApprove() {
        $authorized = Array("gal_auditor", "master_admin");
        if ($this->isAuth($authorized)) {
            $this->load->model("Gallery_model");
            $galObj = $this->Gallery_model->findById($this->input->post("id"), false);
            if ($galObj != null) {
                if ($this->Gallery_model->updateById($galObj->id, Array("is_approved" => true, "approved" => date("Y-m-d H:i:s")))) {
                    echo 1;
                    exit;
                }
            }
        }
        echo 0;
    }

    public function delete() {
        $authorized = Array("gal_auditor", "master_admin");
        if ($this->isAuth($authorized)) {
            $this->load->model("Gallery_model");
            if ($this->Gallery_model->deleteById($this->input->post("id"))) {
                echo 1;
                exit;
            }
        }
        echo 0;
    }

    public function sethl() {
        $authorized = Array("gal_auditor");
        if ($this->isAuth($authorized)) {
            $this->load->model("Gallery_model");
            if ($this->Gallery_model->updateById($this->input->post("id"), Array(
                        "is_featured" => true))) {
                echo 1;
                exit;
            }
        }
        echo 0;
    }

    public function unsethl() {
        $authorized = Array("gal_auditor");
        if ($this->isAuth($authorized)) {
            $this->load->model("Gallery_model");
            if ($this->Gallery_model->updateById($this->input->post("id"), Array(
                        "is_featured" => false))) {
                echo 1;
                exit;
            }
        }
        echo 0;
    }

    public function vote() {
        $point = 0;
        if ($this->_loggedUser != NULL) {
            $vote = $this->input->post("v");
            $this->load->model("Gallery_model");
            $galItem = $this->Gallery_model->findById($this->input->post("gi"));
            if ($galItem != NULL) {
                if ($galItem->is_approved && !$galItem->is_deleted) {
                    $this->load->database("homepage");
                    $this->db->delete('gallery_vote', 'gallery_id = ' . $galItem->id . ' AND user_id = ' . $this->_loggedUser->id);
                    if ($vote == "u") {
                        $this->db->insert('gallery_vote', Array(
                            "gallery_id" => $galItem->id,
                            "user_id" => $this->_loggedUser->id,
                            "point" => 2
                        ));
                        $point += 2;
                    } else if ($vote == "d") {
                        $this->db->insert('gallery_vote', Array(
                            "gallery_id" => $galItem->id,
                            "user_id" => $this->_loggedUser->id,
                            "point" => -1
                        ));
                        $point += -1;
                    }
                }
            }
        }
        echo $point;
    }

}